﻿using Dapper;
using HospitalFollowUpSystem.Entity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;

namespace HospitalFollowUpSystem.DBC
{
    class HRCM_DB
    {
        private String connStr;
        private SqlConnection conn;
        public SqlConnection Database { get => conn; }

        public HRCM_DB()
        {
            connStr = ConfigurationManager.ConnectionStrings["HRCMDB"].ConnectionString;
            conn = new SqlConnection(connStr);
            //HI 记得随时释放连接 db.Database.close();
            conn.Open();
        }

        //插入题目明细
        public Boolean InsertTopic(Topic topic, SqlTransaction trans)
        {
            String sqlStr = @"INSERT INTO dbo.topic
                                      ( groupCode ,
                                        groupName ,
                                        question ,
                                        chioces ,
                                        note ,
                                        questionType ,
                                        operName
                                      )
                              VALUES  ( @GroupCode , 
                                        @GroupName , 
                                        @Questtion , 
                                        @Chioces ,
                                        @Note , 
                                        @QuestionType ,
                                        @OperName    
                                      )";
            DynamicParameters sqlParam = new DynamicParameters();
            sqlParam.Add("@GroupCode",topic.GroupCode);
            sqlParam.Add("@GroupName", topic.GroupName);
            sqlParam.Add("@Questtion",topic.Question);
            sqlParam.Add("@Chioces",topic.Chioces);
            sqlParam.Add("@Note",topic.Note);
            sqlParam.Add("@QuestionType",topic.QuestionType);
            sqlParam.Add("@OperName",topic.OperName);
            bool res = conn.Execute(sqlStr, sqlParam, trans) > 0 ? true : false;
            return res;
        }

        //插入题目分类项
        public Boolean InertTopicGroup(String gourpName, SqlTransaction trans)
        {
            String sqlStr = @"INSERT INTO dbo.topicType
                          ( groupName )
                         VALUES  ( @Name )";

            DynamicParameters sqlParam = new DynamicParameters();
            sqlParam.Add("@Name", gourpName);

            bool res = conn.Execute(sqlStr, sqlParam, trans) > 0 ? true : false;
            return res;

        }

        //获取
        public List<TopicGroup> GetTopicGroup()
        {
            String sqlStr = @"SELECT groupName
                              FROM dbo.topicType(NOLOCK)";
            return conn.Query<TopicGroup>(sqlStr).ToList();
        }

        //获得 账号信息 根据 账号/姓名
        public Operator ReadOperator(String username, String password)
        {
            //Doing 完成此方法
            Operator un = new Operator();
            return un;
        }

        //获得 账号信息 根据 账号/姓名
        public Operator ReadOperator(String username)
        {
            String sqlStr = @"SELECT  *
                              FROM    dbo.Operator
                              WHERE   Username = @Username";
            var sqlParam = new DynamicParameters();
            sqlParam.Add("@Username", username);
            var res = conn.Query<Operator>(sqlStr, sqlParam).FirstOrDefault();
            return res;
        }

        //
        public int ReadOperatorNum(String username)
        {
            String sqlStr = @"SELECT  *
                              FROM    dbo.Operator
                              WHERE   Username = @Username";
            var sqlParam = new DynamicParameters();
            sqlParam.Add("@Username", username);
            var res = conn.Query<Operator>(sqlStr, sqlParam).Count();
            return res;
        }

        // 插入 操作员
        public Boolean InsertOperator(Operator opera)
        {
            //默认生成主键//默认IsStop为false
            String sqlStr = @"INSERT  dbo.Operator
                                      ( Name, Username, Password, Power )
                              VALUES  ( @Name, @Username, @Password, @Power )";
            var sqlParam = new DynamicParameters();
            sqlParam.Add("@Name", opera.Name);
            sqlParam.Add("@Username", opera.Username);
            sqlParam.Add("@Password", opera.Password);
            sqlParam.Add("@Power", opera.Power);
            var res = conn.Execute(sqlStr, sqlParam) > 0 ? true : false;
            return res;
        }

        //密码重置为0
        public int UpDateOperaPWD(String username, String password)
        {
            String sqlStr = @"UPDATE  dbo.Operator
                              SET     Password = @Password
                              WHERE   Username = @Username";
            var sqlParam = new DynamicParameters();
            sqlParam.Add("@Password", password);
            sqlParam.Add("@Username", username);
            var res = conn.Execute(sqlStr, sqlParam);
            return res;
        }

        //停用
        public int UpDateOperaIsStop(String username)
        {
            String sqlStr = @"UPDATE  dbo.Operator
                              SET     IsStop = 1
                              WHERE   Username = @Username";
            var sqlParam = new DynamicParameters();
            sqlParam.Add("@Username", username);
            var res = conn.Execute(sqlStr, sqlParam);
            return res;
        }
    }
}
